<?php

namespace App\Service\Index\v1_3_0;

use App\Helpers\RedisUtil;
use App\Model\Act\ActRechargeLimit;
use App\Model\Act\SigninLog;
use App\Model\Guild\GuildHost;
use App\Model\PayOrder;
use App\Model\User;
use App\Model\UserCoin;
use App\Model\UserFreeCall;
use App\Model\UserProfile;

/**
 * 首页弹窗
新人通话券     注册成功后弹出1分钟免费视频券，可指定用户拨打
拨打视频弹框引导     首页浏览主播列表超过2屏，弹出拨打视频弹框引导；当日浏览同一个主播超过2次，弹出拨打视频引导
新人首充限时福利     注册后浏览过用户主页且账户为0，每次打开app显示固定位置显示➕首页弹框
老用户充值限时福利     充值过但账户为0，每次打开app显示固定位置显示➕首页弹框
男用户连续签到福利     每日首次打开app弹出签到弹框，签到成功后当日不再弹出，并引导任务
主播邀请好友     每日打开app弹出（可选择不再当日不再提醒），提示邀请奖励的分成，鼓励邀请他人入驻
主播主动拨打给男用户     在双方私信页面，放双方亲密值已经达到通话门槛，弹窗引导主播拨打给该男用户（接通后，该话单男用户付费）
男用户主动拨打给主播     在双方私信页面，私信互动超过三句，弹窗引导男用户拨打给该主播，若男用户账户余额不足，点击拨打后引导充值（接通后，该话单男用户付费）
查看消息引导   消息未读超过10条拉起弹框
发布动态引导     浏览动态超过5条弹出发布动态引导
主播完善资料引导  真人认证—实名认证完成后，点击我的弹出引导；照片墙完善—真人认证完成后，照片墙为0情况下，点击我的弹出引导
主页视频通话引导     视频通话按钮做成抖动样式
 */
class PopShowService extends PopBaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        //是否显示弹窗
        $this->showPop = !in_array($this->appId, explode(',', env('SHOW_POP_FORBID_APPID', '6')));
        $profile = UserProfile::getProfile($userId);
        $isHost = UserProfile::checkIsHost($userId, $profile);
        // $rechargeTip = $this->checkRecharge($userId, $profile->gender);
        $fixProfile = $this->checkFixProfile($isHost, $profile);
        $realAvatar = $this->checkRealAvatar($userId, $isHost, $profile);
        $checkMale = $profile->gender == 1 ? 1 : 0;
        $ret['show_list'] = [
            //新人通话券
            ['type' => 'new_call', 'show' => $this->checkNewCall($userId, $profile->gender), 'trigger_num' => 0],
            //拨打视频弹框引导
            ['type' => 'call_video', 'show' => $checkMale, 'trigger_num' => 2],
            //新人首充限时福利
            ['type' => 'new_recharge', 'show' => 0],
            //老用户充值限时福利
            ['type' => 'old_recharge', 'show' => 0],
            //主播邀请好友
            ['type' => 'host_invite', 'show' => $this->checkHostInvite($userId, $isHost)],
            //查看消息引导
            ['type' => 'check_msg', 'show' => 1, 'trigger_num' => 5],
            //发布动态引导
            ['type' => 'pub_moment', 'show' => 1, 'trigger_num' => 5],
            //主播完善资料引导
            ['type' => 'host_fix_profile1', 'show' => $realAvatar],
            //主播完善资料引导2
            ['type' => 'host_fix_profile2', 'show' => $fixProfile],
            //主播视频通话引导
            ['type' => 'host_call_video', 'show' => $isHost],
            //签到
            ['type' => 'sign_in', 'show' => $this->checkSignIn($userId, $profile->gender)],
            //主播新手引导
            ['type' => 'host_new', 'show' => $this->checkHostNew($userId, $profile)]
        ];
        log_user_result($userId, $ret);
        return $ret;
    }

    /**
     * 主播邀请引导
     */
    function checkHostInvite($userId, $isHost = 0)
    {
        if (!$isHost) return 0;
        $key = 'pop_host_invite_' . $userId;
        if (RedisUtil::get($key) == 1) return 0;
        RedisUtil::set($key, 1, 3600 * 3);
        $excluedGuildIds = explode(',', env('EXCLUDE_GUILD', '21'));
        $exludeHost = GuildHost::checkInGuild($userId, 'bool', $excluedGuildIds);
        return $exludeHost ? 0 : 1;
    }

    /**
     * 签到
     */
    function checkSignIn($userId, $gender)
    {
        if ($gender != 1) return 0;
        $checkToday = SigninLog::checkToday(SigninLog::getLastItem($userId));
        return $checkToday ? 0 : 1;
    }

    /**
     * 新用户通话券
     */
    function checkNewCall($userId, $gender)
    {
        return 0;
        if ($gender == 0) return 0;
        $freeCount = UserFreeCall::checkValid($userId, date('Y-m-d'), -2, 'count');
        if ($freeCount > 0) return 0;
        $user = User::getUser($userId);
        return time() - strtotime($user->created_at) < 300 ? 1 : 0;
    }

    /**
     * 新老用户首充福利引导
     */
    function checkRecharge($userId = 0, $gender = 0)
    {
        if ($gender == 0) return ['new' => 0, 'old' => 0];
        $payCount = PayOrder::checkSuccessCount($userId);
        $todayPay = PayOrder::checkSuccessCount($userId, date('Y-m-d'));
        $coinInfo = UserCoin::getUserInfo($userId);
        $remainCoin = $coinInfo['total_coin'] < 10 ? 1 : 0;
        //是否优惠券过期
        $actConf = config('act.recharge.new');
        $remainTime = ActRechargeLimit::getBeginTime($userId, 1) + $actConf['act_time'] - time();
        $checkNew = $payCount ? 0 : ($remainCoin && $remainTime > 0 ? 1 : 0);
        $checkOld = $payCount >= 1 && $todayPay == 0 && $remainCoin ? 1 : 0;
        return [
            'new' => $checkNew,
            'old' => $checkOld
        ];
    }

    function checkFixProfile($isHost = 0, $profile = null)
    {
        if (!$isHost) return 0;
        return !$profile->poster ? 1 : 0;
    }

    function checkRealAvatar($userId = 0, $isHost = 0, $profile = null)
    {
        return 0;
        // if (!$isHost) return 0;
        // return UserProfile::checkAvatarVrfied($userId, $profile);
    }

    /**
     * 主播新手弹窗
     */
    function checkHostNew($userId, $profile = null)
    {
        if ($profile->gender != 0) return 0;
        if ((string)$profile->created_at < config('act.host_task_valid')) return 0;
        $taskInfo = $this->checkHostTask($userId, $profile);
        foreach ($taskInfo as $val) if ($val == 0) return 1;
        return 0;
    }
}
